<?php
 // 2007-11-01 geacosta
 
 /**
 * Makes an alert icon
 */
//function field_error($param, $alertImageURI, $options = array(), $catalogue = 'messages') {
function field_error($param, $alertImageURI, $options = array(), $catalogue = '0') {

   //return "";
  $request = sfContext::getInstance()->getRequest();
  $alertImageOptions = array('id'=>$param.'Error');
  if($request->hasError($param)) {
    $msg = $request->getError($param, $catalogue);
    $alertImageOptions['alt'] = $msg;
    $alertImageOptions['title'] = $msg;
    if(function_exists('my_field_error')) {
      my_field_error($param);
      $alertImageOptions['style'] = 'display: none; border: 0;';
    } else {
      $alertImageOptions['style'] = 'border: 0;';
    }
  } else {
    $alertImageOptions['style'] = 'display: none; border: 0;';
  }

  if($alertImageURI) {
    $src = $alertImageURI;
  } else {
    $src = "iconos/16/menues/importante.png";
  }

  /*Se adicionar para que se muestre los errores con los tooltips 
  * John R.Q.
  */
  $c = new Criteria();
  $error_bd = null;
  if($catalogue != null && $catalogue != 0)
    $error_bd = ErrorPeer::retrieveByPK($catalogue);
    
  if ( $error_bd )
  {
    $msj_error_bd = $error_bd->getErrCadenaError();
    $img_error_bd = $error_bd->getIcono()->getIcoNombre(); 
    $img_tipo_error_bd = $error_bd->getIcono()->getIcoTipoArchivo(); 
    $imagen_html =  image_tag( 'iconos/'.$img_tipo_error_bd.'/'.$img_error_bd.'.'.$img_tipo_error_bd, array('width'=>'30', 'height'=>'30'));
  }
  else 
  {
    $msj_error_bd ="";//"Error: <br> El valor ingresado no es correcto";
    $imagen_html = image_tag( 'iconos/22/mensajes/ico_error.gif', array('width'=>'20', 'height'=>'20'));
  }
  
  $alertImageOptions['onmouseover'] = "var msg = '$msj_error_bd'==''? menjaseError(this) : '$msj_error_bd';  Tip('$imagen_html <br> '+msg+' <br> ', BGCOLOR, '#eeeeee', BORDERCOLOR, '#ee0000', WIDTH, 150, FADEIN, 300, FADEOUT, 300);";

  return '&nbsp;' . image_tag($src, $alertImageOptions);
}


function get_validation_tag_options($name, &$alertImageURI) {
  // Get the module and action of the last created for tag (the form action attribute)
  $lastForm = LastForm::singleton();
  $moduleName = $lastForm->getModule();
  $actionName = $lastForm->getAction();
  if($actionName == "save")$actionName = "edit";
  // If there is a method and action then use the validation configuration file of this method
  // to create variables for client-side validation
  if($moduleName && $actionName) 
  {
    $controller = sfContext::GetInstance()->getController();
    $cacheFile = sfConfig::get('sf_config_cache_dir') . DIRECTORY_SEPARATOR . 'csValidation_' . $moduleName . '_' . $actionName . '.php';
    $configFile = '';

    $dirs = ValidationTools::getValidationDirs($moduleName);
    foreach($dirs as $dir=>$value) 
    {
      if(is_readable($dir . DIRECTORY_SEPARATOR . $actionName . '.yml')) 
      {
        $configFile = $dir . DIRECTORY_SEPARATOR . $actionName . '.yml';
        break;
      }
    }
    

    $cacheOn = sfConfig::get('sf_cache');
    settype($cacheOn, 'bool');
    // If caching is off or the cache file doesn't exist then create a new file
    if((($cacheOn == false) || (!is_readable($cacheFile))) && (is_readable($configFile))) 
    {
      $fh = @fopen($cacheFile, 'w');
      if (!$fh) {
        throw new sfCacheException("Unable to open cache file '$cacheFile' for writing");
      }
      $configParser = new ValidationParser();
      $configParser->initialize(array('required_categories' => array()));
      fwrite($fh, $configParser->execute($configFile));
      fclose($fh);
    }
    // Include the cache file
    $context = sfContext::getInstance();
    $validatorManager = new sfValidatorManager();
    $validatorManager->initialize($context);

    if(is_readable($configFile)) {
      require($cacheFile);
    }
  }
  



 /* if(strrchr ($name, "[" ) != '') 
  {
        $name = str_replace("[", "*", $name);
        $name = str_replace("]", "*", $name);
        $names = explode("*", $name);
        $name = $names[1];

  }*/
  

  // Create attributes for HTML element
  $tagOptions = array();
  $validatorsString = array();

  if(isset($validatorFields[$name])) 
  {
    $required = $validatorFields[$name][0];
    settype($required, 'bool');
    $validatorsString['required'] = $required;
    $validatorsString['group'] = $validatorFields[$name][3];
    if($required) {
      $validatorsString['required_message'] = $validatorFields[$name][1];
    }
    $validatorsString['validators'] = array();
    foreach($fieldValidators[$name] as $fieldValidator) 
    {
        $validatorsString['validators'][] = $validators[$fieldValidator[0]];
    }
    $json = new JSON();
    $tagOptions['alt'] = str_replace("\"", "&#34;", str_replace("\\", "\\\\", $json->encode($validatorsString)));
    
    $events = (isset($fieldEvents[$name]) ? $fieldEvents[$name] : $event);
    if(is_array($events)) {
        foreach($events as $event) {
            $tagOptions[$event] = 'validate(this);';
        }
    }
  }

  return $tagOptions;
}

?>